#!/usr/bin/python
# -*- coding: utf-8 -*-

'''登录世纪佳缘----测试'''


import sys
import json
import requests
from lxml import html


url = 'http://login.jiayuan.com'
session = requests.Session()
request_headers = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                   'Accept-Encoding':'gzip,deflate',
                   'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
                   'Connection':'keep-alive',
                   'Host':'login.jiayuan.com',
                   'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0',
                   }
response = session.get(url, headers=request_headers)

print response.status_code
if response.status_code in (404, 505):
    response.raise_for_status()
    sys.exit(1)
print
for h in response.headers:
    print h, ':', response.headers.get(h)
print
for c in dict(response.cookies):
    print c, ':', response.cookies.get(c)
print
# print response.content

# xpath = r'//*[@id="login"]/input[1]'
# doc = html.document_fromstring(response.text)
# doms = doc.xpath('//*[@id="login"]/input[1]')
# print
# print doms[0].value


post_url = 'https://passport.jiayuan.com/dologin.php?pre_url=http://www.jiayuan.com/usercp'
request_headers = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                   'Accept-Encoding':'gzip,deflate',
                   'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
                   'Connection':'keep-alive',
                   'cookies':'SESSION_HASH=%s; user_access=1' % response.cookies.get('SESSION_HASH'),
                   'Host':'passport.jiayuan.com',
                   'Referer':'http://login.jiayuan.com/',
                   'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0',
                   }
form = {'name':'baixuexue123@sina.com',
        'password':'xuebailove321',
        '_s_x_id':response.cookies.get('PHPSESSID'),
        'ljg_login':'1',
        'm_p_l':'1',
        'channel':'0',
        'position':'0',
        }

response = session.post(post_url, form, headers=request_headers, verify=True, allow_redirects=True)

print response.status_code
if response.status_code in (404, 505):
    response.raise_for_status()
    sys.exit(1)
print response.content


















